package base.web.tools.result;

import java.text.SimpleDateFormat;
import java.util.Date;

import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

public class DateJsonValueProcessor implements JsonValueProcessor
{

	private String dateFormat = "yyyy-MM-dd";

	public String getDateFormat()
	{
		return dateFormat;
	}

	public void setDateFormat(String dateFormat)
	{
		this.dateFormat = dateFormat;
	}

	public DateJsonValueProcessor()
	{
	}

	public DateJsonValueProcessor(String format)
	{
		this.dateFormat = format;
	}

	public Object processArrayValue(Object value, JsonConfig jsonConfig)
	{
		String[] obj = {};
		if (value != null)
		{
			if (value instanceof Date[])
			{
				SimpleDateFormat sf = new SimpleDateFormat(dateFormat);
				Date[] dates = (Date[]) value;
				obj = new String[dates.length];
				for (int i = 0; i < dates.length; i++)
				{
					obj[i] = sf.format(dates[i]);
				}
			}
		}
		return obj;
	}

	public Object processObjectValue(String key, Object value, JsonConfig jsonConfig)
	{
		if (value != null)
		{
			if (value instanceof Date)
			{
				String str = new SimpleDateFormat(dateFormat).format((Date) value);
				return str;
			}
		}
		return value;
	}

	

}
